package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import com.fwtai.web.SmsTemplateDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 短信模板业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-11-10 14:33:39
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class SmsTemplateService{

    @Resource
    private SmsTemplateDao smstemplateDao;

    public String add(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_template_code = "template_code";
        final String p_template_name = "template_name";
        final String validate = ToolClient.validateField(formData,p_template_code,p_template_name);
        if(validate != null)return validate;
        if(formData.getString(p_template_code).length() > 128){
            return ToolClient.createJsonFail("模板编号内容字数太长");
        }
        if(formData.getString(p_template_name).length() > 128){
            return ToolClient.createJsonFail("模版名称内容字数太长");
        }
        formData.put("kid",new IdBuilder().getId121L());
        return ToolClient.executeRows(smstemplateDao.add(formData));
    }

    public String edit(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_kid = "kid";
        final String p_template_code = "template_code";
        final String p_template_name = "template_name";
        final String validate = ToolClient.validateField(formData,p_template_code,p_template_name,p_kid);
        if(validate != null)return validate;
        if(formData.getString(p_template_code).length() > 128){
            return ToolClient.createJsonFail("模板编号内容字数太长");
        }
        if(formData.getString(p_template_name).length() > 128){
            return ToolClient.createJsonFail("模版名称内容字数太长");
        }
        final String exist_key = smstemplateDao.queryExistById(formData.getString(p_kid));
        if(exist_key == null){
            return ToolClient.createJsonFail("数据不存在,刷新重试");
        }
        return ToolClient.executeRows(smstemplateDao.edit(formData));
    }

    public String queryById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        return ToolClient.queryJson(smstemplateDao.queryById(kid));
    }

    public String delById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final String exist_key = smstemplateDao.queryExistById(kid);
        if(exist_key == null){
            return ToolClient.createJsonFail("删除失败,数据已不存在");
        }
        return ToolClient.executeRows(smstemplateDao.delById(kid));
    }

    public String editDisplay(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_kid = "kid";
        final String p_enabled = "enabled";
        final String validate = ToolClient.validateField(formData,p_enabled,p_kid);
        if(validate != null)return validate;
        final String fieldInteger = ToolClient.validateInteger(formData,p_enabled);
        if(fieldInteger != null)return fieldInteger;
        final String exist_key = smstemplateDao.queryExistById(formData.getString(p_kid));
        if(exist_key == null){
            return ToolClient.createJsonFail("数据不存在,刷新重试");
        }
        return ToolClient.executeRows(smstemplateDao.editDisplay(formData));
    }

    public String delByKeys(final String ids){
        if(ids == null || ids.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final ArrayList<String> lists = ToolString.keysToList(ids);
        if(lists == null || lists.size() <= 0){
            return ToolClient.createJsonFail("请选择要删除的数据");
        }
        return ToolClient.executeRows(smstemplateDao.delByKeys(lists),"操作成功","数据不存在,刷新重试");
    }

    public String listData(PageFormData formData){
        try {
            formData = ToolClient.dataTableMysql(formData);
            if(formData == null)return ToolClient.jsonValidateField();
            final HashMap<String,Object> map = smstemplateDao.listData(formData);
            return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
        } catch (Exception e){
            return ToolClient.dataTableException(formData.get("sEcho"));
        }
    }
}